﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace SPVisualDev.VSAddin.Dialogs.PropertySheets
{
    internal class PropSheetListInstance : PropSheetBase
    {
        public PropSheetListInstance(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, ElementInfo Element)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            this.Title = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Url = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            this.ElementInfo = Element;
            this.ElementFileName = Element.ElementFileName;


            if (Element.XmlElementNode != null)
                this.PropXmlNode = Element.XmlElementNode;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Instance ID"), RefreshProperties(RefreshProperties.None)]
        public string Id
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Title"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Title
        {
            get;
            set;
        }


        [CategoryAttribute("Properties"), DescriptionAttribute("Url"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Url
        {
            get;
            set;
        }

        private string m_TemplateType = "0";
        [CategoryAttribute("Properties"), DescriptionAttribute("Custom list template type"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(TemplateTypeExpandableConverter))]
        public string TemplateType
        {
            get
            {
                return m_TemplateType;
            }
            set
            {
                try
                {
                    //Test if it is a valid integer.
                    int i = int.Parse(value);

                    string s = Enum.GetName(typeof(ListTemplateTypeEnum), i);
                    if (s != null)
                    {
                        m_TemplateType = s;
                    }
                    else
                    {
                        m_TemplateType = value;
                    }

                }
                catch
                {
                    try
                    {
                        ListTemplateTypeEnum type = (ListTemplateTypeEnum)Enum.Parse(typeof(ListTemplateTypeEnum), value);
                        this.FeatureId = Common.GetFeatureIdForBuiltInListType((int)type).ToString().ToUpper();
                    }
                    catch { }

                    m_TemplateType = value;
                }

            }
        }


        [CategoryAttribute("Properties"), DescriptionAttribute("Feature ID"), RefreshProperties(RefreshProperties.None)]
        public string FeatureId
        {
            get;
            set;
        }

        public override string ToString()
        {
            string s = this.Url.GetValue(null);
            if (string.IsNullOrEmpty(s))
                s = this.ElementInfo.ElementName + this.ElementInfo.ID.ToString();

            return "<ListInstance> " + s;
        }

        [Browsable(false)]
        public override XmlElement PropXmlNode
        {
            get
            {
                XmlElement xel = base.PropXmlNode;

                try
                {
                    int.Parse(xel.GetAttribute("TemplateType"));
                }
                catch (FormatException)
                {
                    try
                    {
                        ListTemplateTypeEnum type = (ListTemplateTypeEnum)Enum.Parse(typeof(ListTemplateTypeEnum), xel.GetAttribute("TemplateType"), false);

                        xel.SetAttribute("TemplateType", ((int)type).ToString());
                    }
                    catch { }
                }


                return xel;
            }
            set
            {
                base.PropXmlNode = value;

                XmlElement xel = value;

                string s = xel.GetAttribute("Id");
                if (string.IsNullOrEmpty(s))
                {
                    this.Id = new Random().Next(65535).ToString();
                }
                else
                {
                    try
                    {
                        this.Id = s;
                    }
                    catch { }
                }

                /*
                ListTemplateTypeEnum type = ListTemplateTypeEnum.InvalidType;
                s = xel.GetAttribute("TemplateType");
                if (!string.IsNullOrEmpty(s))
                {
                    if (Enum.IsDefined(typeof(Microsoft.SharePoint.ListTemplateTypeEnum), int.Parse(s)))
                    {
                        type = (ListTemplateTypeEnum)Enum.Parse(typeof(ListTemplateTypeEnum), s);
                    }
                }

                this.TemplateBuiltInType = type;


                if (type != ListTemplateTypeEnum.InvalidType)
                {
                    this.TemplateType = ((int)type).ToString();
                }
                else
                {
                    this.TemplateType = s;
                }
                */
            }
        }

        public class TemplateTypeExpandableConverter : StringConverter
        {
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                return false;
            }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                StandardValuesCollection coll = new StandardValuesCollection(Enum.GetNames(typeof(ListTemplateTypeEnum)));
                return coll;
            }
        }
    }

}
